﻿using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Data;
using CommonTools;
using shop.Model;
using System.Threading.Tasks;

namespace shop.Bll
{

	/// <summary>  </summary>
  	public class ShopLoginBll : BasePageBll<ShopLogin>
    {
        public override string Title => "店铺登录信息";

        public override bool EnableEditOnList => true;


        public override bool RefreshOnOpen => true;

        public override ShopLogin CopyItem(ShopLogin item)
        {
            if (item == null)
                return null;

            var newItem = item.Clone();
            newItem.Id = 0;
            newItem.isdel = false;

            return newItem;
        }

        public override ShopLogin CreateItem()
        {
            return new ShopLogin() { isdel = false, FUser = UserBll.CurrentUser.Name, FTime = DateTime.Now, };
        }

        public override async Task<Result> DeleteItem(int id)
        {
            await DB.FSql.Update<ShopLogin>()
                .Where(a => a.Id == id && a.isdel == false)
                .Set(a => a.isdel, true)
                .ExecuteAffrowsAsync();

            //await DB.FSql.Insert(new CLogs()
            //{
            //    DocName = nameof(ShopLogin),
            //    Computer = UserBll.CurrentUser.ComputerInfo,
            //    Zdr = UserBll.CurrentUser.Name,
            //    DocId = id.ToString(),
            //    DocAction = "删除",
            //})
            //    .ExecuteAffrowsAsync();

            return new Result(true);
        }


        public override async Task<Result<int>> EditCell(string dataPropertyName, object value, ShopLogin data)
        {
            if (data.Id == 0)
            {
                data.FTime = await DB.FSql.GetTimeAsync();
                data.FUser = UserBll.CurrentUser.Name;
                data.isdel = false;

                var nid = await DB.FSql.Insert(data)
                    .ExecuteIdentityAsync();

                data.Id = (int)nid;
            }

            var dict = new Dictionary<string, object> { [dataPropertyName] = value };



            var dbRes = await DB.FSql.Update<ShopLogin>()
                .Where(a => a.Id == data.Id && a.isdel == false)
                .SetDto(dict)
                .ExecuteAffrowsAsync();

            //if (dataPropertyName == nameof(ShopLogin.Password))
            //{
            //    data.Password = "******";
            //}

            return new Result<int>(true, "!", data.Id);
        }

        public override IEnumerable<DeclareColumn> GetColumns()
        {
            return new[]
            {
                new DeclareColumn(nameof(ShopLogin.Id),"Id"){ Visible= false},
                new DeclareColumn(nameof(ShopLogin.Shop),"店铺"){ Width=260 },
                new DeclareColumn(nameof(ShopLogin.UserName),"登录账号"){ Width=180, },
                //new DeclareColumn(nameof(ShopLogin.Password),"登录密码"){ Width=120,  ReadOnly= true},
                new DeclareColumn(nameof(ShopLogin.UserPhone),"验证码电话"){ Width=140, },
                new DeclareColumn(nameof(ShopLogin.Remarks),"备注"){ Width=200, },
                new DeclareColumn(nameof(ShopLogin.FTime),"登记日期"){ Width=110, ValueType=typeof(DateTime) },
            };
        }

        public override async Task<ShopLogin> GetItem(int id)
        {
            return await DB.FSql.Select<ShopLogin>()
                .Where(a => a.isdel == false && a.Id == id)
                .ToOneAsync();
        }


        #region 查询参数

        private string _shop;
        private string _user;

        #endregion

        /// <summary> 查询ui </summary>
        public override ControlBuilder GetUIBuilder()
        {
            return new ControlBuilder()
                .Add<string>(ControlEnum.guanTextBox, "店铺名称", c => _shop= c)
                .Add<string>(ControlEnum.guanTextBox, "登录账号", c => _user = c)
                .AddQueryButton("查询");
        }

        /// <summary> 查询 </summary>
        public override async Task<PageQueryResult<ShopLogin>> QueryPage(DocFilterParameter queryPara)
        {
            var list = await DB.FSql.Select<ShopLogin>()
                .Where(a => a.isdel == false)
                .WhereIf(!string.IsNullOrEmpty(_shop), a => a.Shop.Contains(_shop) )
                .WhereIf(!string.IsNullOrEmpty(_user), a => a.UserName.Contains(_user) )
                .Page(queryPara.PageNo, queryPara.PageSize)
                .OrderBy(a => a.FTime)
                .OrderBy(a => a.Shop)
                .Count(out var c)
                .ToListAsync();

            foreach (var item in list)
            {
                if(!string.IsNullOrEmpty(item.Password))
                {
                    item.Password = "******";
                }
            }
            return new PageQueryResult<ShopLogin>(true, null, list, c);
        }



        public override async Task<Result> SaveItem(ShopLogin item)
        {
            if (item == null)
            {
                return new Result(false, "系统错误");
            }

            string err = string.Empty;
            if (string.IsNullOrEmpty(item.Shop))
            {
                err += "店铺名称不能为空!";
            }

            if (string.IsNullOrEmpty(err) == false)
            {
                return new Result(false, err);
            }

            item.isdel = false;
            item.FTime = await DB.FSql.GetTimeAsync();
            item.FUser = UserBll.CurrentUser.Name;

            if (item.Id <= 0)
            {
                var id = await DB.FSql.Insert(item)
                    .ExecuteIdentityAsync();

                item.Id = (int)id;
            }
            else
            {
                await DB.FSql.Update<ShopLogin>()
                    .SetSource(item)
                    .ExecuteAffrowsAsync();
            }

            return new Result(true);
        }



    }




}



 
